/**
 author: chengzheng
 date: 2017/12/26
 email: 13120207061@163.com
 */
const puppeteer = require("puppeteer");
const logger = require("../../logger");
let browserInst = null;
let retryLimit = 10;

module.exports.request = async function(option){
    if(!browserInst){
        logger.trace('headless chromium will launch');

        browserInst = await  puppeteer.launch({
            args:[
                '--no-sandbox','--disable-setuid-sandbox'
            ],
            ignoreHTTPSErrors:true
        });
        logger.info("headless chromium launch");
        process.on("exit",function () {
            if(browserInst){
                browserInst.close();
            }
        });
    }

    let page = await  browserInst.newPage();
    logger.info(`browser create new page for url:${option.url}`);
    await page.setViewport(option.viewPort);
    await page.setUserAgent(option.userAgent);


    if(!!option.retryLimit){
        retryLimit = option.retryLimit;
    }

    let requestSuccess = false;
    let retry = 0;
    do {
        if (retry > retryLimit) {
            logger.error(`browser retry out of limit, url: ${option.url}`);
            page.close();
            return 'crawl failed';
        }

        if (retry !== 0 && retry <= retryLimit) {
            logger.debug(`browser request retry: ${retry}`);
        }
        try {
            logger.trace(`browser will request for url: ${option.url}`);
            await page.goto(option.url, {waitUntil: 'networkidle'});
            logger.trace(`browser request finish for url: ${option.url}`);
            requestSuccess = true;
        } catch (err) {
            requestSuccess = false;
            retry++;
            logger.fatal(`browser request to url ${option.url} has error: ${err.toString()}`);
        }
    }
    while(!requestSuccess);

    let html = await page.content();
    logger.trace(`browser get page content: ${option.url}`);
    page.close();
    logger.trace(`browser page close`)
    return html;


}

